var userFlag = false;
$("#username").on("focus",function()
{
	$("#username").on("blur",function()
	{
		$.ajax
		({
			type:"get",
			url:"/usernameExist",
			data:
			{
				username: $("#username").val()
			},
			success:function(res) 
			{ 
				if(res.state=="2")
				{
					//用户名不存在
					$(".userExistText").css("display","inline-block");
					$(".userExistText>img").attr({"src":"images/false.gif","title":"用户名不存在",});
				}
				if(res.state=="0")
				{
					//用户名存在可以登录
					userFlag = true;
					$(".userExistText").css("display","inline-block");
					$(".userExistText>img").attr({"src":"images/ok.gif","title":"用户名存在"});
				}
			}
		});
	});
});
$("input[type='button']").on("click",function(e)
{
	var username = $("#username").val();
	var myPassword = $("#password").val();
	console.log(username+","+myPassword);
	if(myPassword.length<=0||username.length<=0)
	{
		alert("用户名或者密码不能为空");
		return;
	}else if(userFlag == false)
	{
		alert("您输入的用户名不存在");
		return;
	}else
	{
		$.ajax(
		{
			url:"/login",
			type: "POST",
			data: {username:username,psw:myPassword},
			error:function(err)
			{
				console.log(err);
			},
			success:function(res)
			{
				console.log(res);
				if(res.state == "0")
				{
					alert(res.msg);
					location.href = "/";
					return;
				}
				if(res.state == "1")
				{
					alert(res.msg);
  				}
			}
		})
	}
})

